package comm_ctl

import (
	"net/http"
	"ubt-rbac-service/internal/app/model"
	"ubt-rbac-service/internal/app/service/comm_serv"
	"ubt-rbac-service/internal/app/util"
)

// 一些通用的接口
type comm struct{}

var CommCtl = comm{}

// ImgCode 生成图形验证码
func (c *comm) ImgCode(w http.ResponseWriter, _ *http.Request) {
	code := util.NewImage(100, 38)
	if code != "" {
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: code})
	} else {
		util.ToJson(w, &model.ResJson{Code: 500, Msg: "对不起，系统错误：图形验证码生成失败！"})
	}
}

// Areas 下拉选择省市区乡镇
func (c *comm) Areas(w http.ResponseWriter, r *http.Request) {
	u := r.URL.Query()
	if u == nil {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数不能为空，请重试"})
		return
	}
	// 过滤关键参数值中的非法字符
	pid := u.Get("pid")
	pid = util.InputFilter(pid, true)
	if pid == "" {
		util.ToJson(w, &model.ResJson{Code: 400, Msg: "对不起，请求的参数不能为空，请重试"})
		return
	}
	result := comm_serv.CommServ.Areas(pid)
	if result != nil && result.RetCode == 1001 {
		util.ToJson(w, &model.ResJson{Code: 200, Msg: "ok", Data: result.RetData})
	} else {
		util.ToJson(w, &model.ResJson{Code: 404, Msg: result.RetMsg})
	}
}
